/*******************************************************************************
 * Copyright (c) 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package com.takaharabooks.mhp3rd;

import jp.co.nobot.libAdMaker.libAdMaker;

import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.graphics.Color;
// text
import android.text.style.RelativeSizeSpan;
import android.text.Spanned;
import android.text.Html;
// view
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.AdapterView;
import android.content.res.Resources;

// Dialog
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
// text
import android.text.SpannableString;


public class HuntingWhistleActivity extends Activity implements Runnable
{
	// 広告クラス
	private ConbinateAdView m_csAd;
    // DB
    private DB_HuntingWhistle m_dbHuntingWhistle = null;	
	// プログレスダイアログ
	static private ProgressDialog waitDialog;
	// 現在の音符位置
	private int m_nNowWhistle = 0;
	private View m_csWhistleViews[];
	// 絞り込み用
	private Spinner m_csKindSpinner;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hunting_whistle);   

        // 広告表示
        m_csAd = new ConbinateAdView( (ProgressBar)findViewById(R.id.loading2) );
        m_csAd.InitAdMaker((libAdMaker)findViewById(R.id.admakerview2), this);
        m_csAd.showAdMaker();
        
	}

    @Override
    public void onStart()
    {
    	// プログレスダイアログ初期化
    	InitWaitDialog();
        
        super.onStart();
    }
    
    public void InitWaitDialog()
    {
    	// プログレスダイアログの削除
    	dismissProgressDlg();  		
    	
        // 読み込み中のプログレスダイアログを表示
        waitDialog = new ProgressDialog(this);
        waitDialog.setTitle("Loading...");
        waitDialog.setMessage("データ読み込み中です。\nしばらくお待ちください...");
        waitDialog.setIndeterminate(false);
        waitDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        waitDialog.show();    	

        // 処理開始
        Thread thread = new Thread(this);
        thread.start();
    }
    
    @Override
    public void onPause()
    {
    	// プログレスダイアログの削除
    	dismissProgressDlg();
    	super.onPause();
    }

    @Override
    public void onDestroy(){
    	if( null != m_dbHuntingWhistle ){
    		m_dbHuntingWhistle.CloseDB();    	
    	}
    	super.onDestroy();
    }

    @Override
	public void run()
    {
        // 時間のかかる処理をここに記述。    	
        // データベースを作成
    	if( null == m_dbHuntingWhistle ){
    		m_dbHuntingWhistle = new DB_HuntingWhistle(this); 
    		m_dbHuntingWhistle.InitDB();
    	}
        handler.sendEmptyMessage(0);
    }
    
    private Handler handler = new Handler()
    {
        @Override
		public void handleMessage(Message msg)
        {
            // 処理終了時の動作をここに記述。
        	
            // 笛リスト初期
        	InitWhistle();

        	// 絞込み機能初期化
        	InitKindSpinner();
        	
        	// プログレスダイアログの削除
        	dismissProgressDlg();  		
        }
    };    
    
	// プログレスダイアログの削除
    private void dismissProgressDlg()
    {
		if(waitDialog != null && waitDialog.isShowing()){
			waitDialog.dismiss();
			waitDialog = null;
		}    	
    }
    
    /**
     * メニューを作成する
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        super.onCreateOptionsMenu(menu);

        // タイトルメニューの作成
        MenuItem csMenu1 = menu.add(Menu.NONE, Menu.FIRST, Menu.NONE, "タイトルへ");
        csMenu1.setIcon(android.R.drawable.ic_menu_revert);
        MenuItem csMenu2 = menu.add(Menu.NONE, Menu.FIRST+1, Menu.NONE, "フルスクリーン");
        csMenu2.setIcon(android.R.drawable.ic_menu_view);
        
        return true;
    }
   /**
     * メニューボタン押下時の処理
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem menu)
    {
        switch (menu.getItemId()) {
        case Menu.FIRST:
            //Activity終了
            finish();
            return true;
        case Menu.FIRST+1:
            //フルスクリーン処理
        	if(0 == (getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN)){
	        	getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
	        	menu.setTitle("通常の画面");
        	}else{
                //フルスクリーンなら元に戻す
	        	getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
	        	menu.setTitle("フルスクリーン");
        	}
            return true;
        default:
            break;
        }
        
        return super.onOptionsItemSelected(menu);
    }

    /*************************************
     * 笛リストの初期化
     **************************************/
    public void InitWhistle()
    {
    	Resources res = getResources();
    	LinearLayout csLayout = (LinearLayout) findViewById(R.id.HuntingWistleLayout);
    	csLayout.removeAllViews();
    	
    	int nWhistleNum = m_dbHuntingWhistle.GetWhistleNum();
    	m_csWhistleViews = new View[nWhistleNum];

    	for(int nNum=0; nNum<nWhistleNum; nNum++){
            // 色変更
            CharSequence source = Html.fromHtml(m_dbHuntingWhistle.GetWhistleStr(nNum));
            
            // リサイズ
            SpannableString spannable = new SpannableString(source);
            RelativeSizeSpan spanResize = new RelativeSizeSpan(1.6f); 
            spannable.setSpan(spanResize, 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);		

    		
    		TextView csView = new TextView(this);
	    	csView.setText(spannable);
	    	csView.setLayoutParams(csLayout.getLayoutParams());
	    	csView.setBackgroundDrawable(res.getDrawable(R.drawable.frame00));
	    	csLayout.addView(csView,
	    			new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
	    										  ViewGroup.LayoutParams.WRAP_CONTENT));
	    	m_csWhistleViews[nNum] = csView;
	    	
	    	// 現在表示中の値を変更
	    	m_nNowWhistle = nNum;
	    	
	    	// クリックされたときの動作を定義
	    	csView.setOnClickListener(new View.OnClickListener(){
	        	Resources res = getResources();
	    		private int m_nNum = m_nNowWhistle;
	    		@Override
	    		public void onClick(View v){
	    			m_csWhistleViews[m_nNowWhistle].setBackgroundDrawable(res.getDrawable(R.drawable.frame00));
	    			m_nNowWhistle = m_nNum;
	    			InitHuntingWhistle(m_nNum);
	    			m_csWhistleViews[m_nNum].setBackgroundDrawable(res.getDrawable(R.drawable.frame00));
	    			m_csWhistleViews[m_nNum].setBackgroundColor(Color.BLACK);
	    			//m_csWhistleViews[m_nNum].setBackgroundColor(Color.argb(0xa0, 0xcc, 0xcc, 0xcc));
	    		}
	    	});
    	}
    	
    	// 初期化
    	m_nNowWhistle = 0;  
    	InitHuntingWhistle(0);
		m_csWhistleViews[0].setBackgroundColor(Color.BLACK);
    }
    
    /*************************************
     * 早見表リストの初期化
     **************************************/
    public void InitHuntingWhistle(int nIndex)
    {
    	LinearLayout csLayout = (LinearLayout) findViewById(R.id.HuntingWistleDescLayout);
    	csLayout.removeAllViews();

		String[][] strDesc = m_dbHuntingWhistle.GetHuntingWhistleStr(nIndex);
		
    	for(int nNum=0; nNum<strDesc.length; nNum++){
    		String strHtml = "";
    		strHtml += strDesc[nNum][0] + "<br>";
    		strHtml += "<font color=white>" + strDesc[nNum][1] + "</font>";
    		if( 0 < strDesc[nNum][2].length() ){
    			strHtml += "<br><font color=yellow>" + strDesc[nNum][2] + "</font>";
    		}
    		
            // 色変更
            CharSequence source = Html.fromHtml(strHtml);
            
            // リサイズ
            SpannableString spannable = new SpannableString(source);
            RelativeSizeSpan spanResize = new RelativeSizeSpan(1.5f); 
            spannable.setSpan(spanResize, 0, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);		

    		
    		TextView csView = new TextView(this);
	    	csView.setText(spannable);
	    	csView.setLayoutParams(csLayout.getLayoutParams());
	    	if(nNum%2==0){
	    		csView.setBackgroundColor(Color.argb(0xa0, 0x60, 0x60, 0x60));
	    	}else{
	    		csView.setBackgroundColor(Color.argb(0xa0, 0xa0, 0xa0, 0xa0));	    		
	    	}
	    	csLayout.addView(csView,
	    			new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
	    										  ViewGroup.LayoutParams.WRAP_CONTENT));
    	}
    }
    
    /*************************************
     * 旋律効果種別絞込み機能の初期化
     **************************************/
    public void InitKindSpinner()
    {
    	m_csKindSpinner = (Spinner) findViewById(R.id.HuntingWhistleKindSpinner);
    	m_csKindSpinner.setSelection(0);
  		m_csKindSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
  		{  			
  			@Override
    		public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
    			// データベースを作り変える
  				m_dbHuntingWhistle.ResetDB(position);
  		    	// 早見表初期化
  	        	InitWhistle();
  			}
  			
  			@Override
  			public void onNothingSelected(AdapterView<?> arg0) {
            }
    	});    	
    }    
}